﻿using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TUlingWpf.Heplers;
using TUlingWpf.Messages;
using TUlingWpf.ViewModels;
using TUlingWpf.Views;

namespace TUlingWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = App.Current?.Service?.GetService<MainWindowViewModel>();
            InitLoginView();
        }

        private void InitLoginView()
        {
            MainView.Content = App.Current?.Service?.GetService<LoginView>();

            //登录成功则跳转
            //如果登录成功，就跳转到主界面上（来自于LoginViewModel的消息注册）
            WeakReferenceMessenger.Default.Register<LoginMessage>(this, (send, arg) =>
            {
                MainView.Content = App.Current?.Service?.GetService<HomeView>();
            });

            //登出消息
            WeakReferenceMessenger.Default.Register<LogOutMessage>(this, (send, arg) =>
            {
                MainView.Content = App.Current?.Service?.GetService<LoginView>();
                //Width=1200;
                //Height=800;
                Debug.WriteLine("mainwindow被打印");
            });
        }

        private void SetWindowLocation()
        {
            Left = (SystemParameters.WorkArea.Width-Width)/3;
            Top = (SystemParameters.WorkArea.Height-Height)/3;
        }

        private void MainWindow_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.OriginalSource is TextBox || e.OriginalSource is PasswordBox)
            {
                return;
            }

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove(); // 触发窗体拖动
            }
        }
    }
}